package com.liuxu.NoRoot;

import android.Manifest;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    public boolean check=true;
    public static String version;
    private int index;
    private int currentTabIndex=0;
    private Fragment[] mFragments;
    public static List<CheckBox> checkBoxList;
    public Button button1;
    public Button button2;
    public Button button3;
    public BottomNavigationBar bottomNavigationBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        initView();
        initBottomNavigationBar();
        initListener();
    }
    private void initListener() {

    }

    private void initBottomNavigationBar() {
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar
                .setMode(BottomNavigationBar.MODE_FIXED)
                /**
                 *  setMode() 内的参数有三种模式类型：
                 *  MODE_DEFAULT 自动模式：导航栏Item的个数<=3 用 MODE_FIXED 模式，否则用 MODE_SHIFTING 模式
                 *  MODE_FIXED 固定模式：未选中的Item显示文字，无切换动画效果。
                 *  MODE_SHIFTING 切换模式：未选中的Item不显示文字，选中的显示文字，有切换动画效果。
                 */
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                /**
                 *  setBackgroundStyle() 内的参数有三种样式
                 *  BACKGROUND_STYLE_DEFAULT: 默认样式 如果设置的Mode为MODE_FIXED，将使用BACKGROUND_STYLE_STATIC
                 *                                    如果Mode为MODE_SHIFTING将使用BACKGROUND_STYLE_RIPPLE。
                 *  BACKGROUND_STYLE_STATIC: 静态样式 点击无波纹效果
                 *  BACKGROUND_STYLE_RIPPLE: 波纹样式 点击有波纹效果
                 */
                .setActiveColor(R.color.colorPrimaryDark) //选中颜色
                .setInActiveColor(R.color.white) //未选中颜色
                .setBarBackgroundColor(R.color.gray);//导航栏背景色

        /** 添加导航按钮 */
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.home, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.menu, "说明"))
                .setFirstSelectedPosition(0)
                .initialise(); //initialise 一定要放在 所有设置的最后一项
        FragmentTransaction firsttrx = getSupportFragmentManager().beginTransaction();
        firsttrx.replace(R.id.fl, mFragments[0]);
        firsttrx.commit();
        bottomNavigationBar //设置lab点击事件
                .setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {

                    @Override
                    public void onTabSelected(int position) {
                        Log.d("标签", "onTabSelected() called with: " + "position = [" + position + "]");
                        switch (position){
                            case 0:
                                index = 0;
                                break;
                            case 1:
                                index = 1;
                                break;
                        }
                        if (currentTabIndex != index) {
                            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                            trx.hide(mFragments[currentTabIndex]);
                            if (!mFragments[index].isAdded()) {
                                trx.add(R.id.fl, mFragments[index]);
                            }
                                trx.show(mFragments[index]);
                            trx.commit();
                        }
                        currentTabIndex = index;
                    }

                    @Override
                    public void onTabUnselected(int position) {

                    }

                    @Override
                    public void onTabReselected(int position) {

                    }
                });

    }

    private void initView() {
        mFragments=new Fragment[]{new HomeFragment(),new InfoFragment()};
    }

    public boolean checkAccessibilityEnabled(String serviceName) {
        AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
        List<AccessibilityServiceInfo> accessibilityServices =accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        for (AccessibilityServiceInfo info : accessibilityServices) {
            Log.d("无障碍", info.getId().toString());
            if (info.getId().equals(serviceName)) {
                Log.d("超级头条无障碍服务检测：", "服务已开启>" );
                return true;
            }
        }
        return false;
    }

    public Handler handler = new Handler(){
        //写handler的handlermessage方法,用来接收子线程中发来的消息
        public void handleMessage(android.os.Message msg) {
            String text=(String)msg.obj;
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
        };
    };

    public static String getVersion(Context Context) {
        String verName = "";
        try {
            verName = Context.getPackageManager().getPackageInfo(Context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

    public void tokenSave(String token ) {
        SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();//获取到edit对象
        edit.putString("token",token); //通过editor对象写入数据
        edit.commit();//提交数据存入到xml文件中
    }

    public void tokenSave(List<CheckBox> checkBoxList) {
        SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();//获取到edit对象
        for(int i=0;i<checkBoxList.size();i++) {
            edit.putBoolean(checkBoxList.get(i).getText().toString(),checkBoxList.get(i).isChecked());
        }
        edit.commit();//提交数据存入到xml文件中
    }

    public void tokenSet(List<CheckBox> checkBoxList) {
        SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
        for(int i=0;i<checkBoxList.size();i++) {
            boolean temp=sp.getBoolean(checkBoxList.get(i).getText().toString(),false);
                checkBoxList.get(i).setChecked(temp);
        }
    }

    public void tokenSet(EditText editText ) {
        SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
        String value = sp.getString("token","");
        if (!value.equals("")){
            editText.setText(value);
        }
    }

    public String getSerial() {
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return "unknow";
        }
        String phoneInfo = TelephonyMgr.getDeviceId();
        return String.valueOf(phoneInfo);
    }




}